﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using WebTraderLiteCL;

namespace WebTraderLite.Pages
{
    public partial class AutoBuy : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                if (Request.QueryString["stock"] != null)
                {
                    tbStockSym.Text = Request.QueryString["stock"].ToUpper();
                }
            }
        }

        protected void showConfirmautobuy(object sender, EventArgs e)
        {
            //Show confirm autobuy view
            MultiViewautobuyStock.ActiveViewIndex = 1;
            TextBox tbNumShares = fvautobuyStock.FindControl("tbNumShares") as TextBox;
            Label lblNumShares = fvConfrimautobuy.FindControl("lblNumShares") as Label;
            lblNumShares.Text = tbNumShares.Text;

            //Show info on trend and autobuy price user selected from other view
            DropDownList drpListTrend = fvautobuyStock.FindControl("trendList") as DropDownList;
            Label lblTrend = fvConfrimautobuy.FindControl("lblTrend") as Label;
            lblTrend.Text = drpListTrend.SelectedItem.Text;

            TextBox tbTrigPrice = fvautobuyStock.FindControl("tbTriggerPrice") as TextBox;
            Label lblTrigPrice = fvConfrimautobuy.FindControl("lblTriggerPrice") as Label;
            float buyPrice = float.Parse(tbTrigPrice.Text);
            lblTrigPrice.Text = buyPrice.ToString("C");
            


            //Calculate (potential) total order price
            float orderTotal = buyPrice * float.Parse(lblNumShares.Text);
            Label totPrice = fvConfrimautobuy.FindControl("lblTotPrice") as Label;
            totPrice.Text = orderTotal.ToString("C");
        }

        protected void processautobuy(object sender, EventArgs e)
        {

            Label lblSymbol = fvConfrimautobuy.FindControl("lblStockSym") as Label;//stocksymbol
           
            Label lblNumShares = fvConfrimautobuy.FindControl("lblNumShares") as Label;
            float numSharesToautobuy = float.Parse(lblNumShares.Text);//noofshare

            DropDownList drpListTrend = fvautobuyStock.FindControl("trendList") as DropDownList;
            Label lblTrend = fvConfrimautobuy.FindControl("lblTrend") as Label;//trend
            string trend=lblTrend.Text.Trim().ToUpper();

            TextBox tbTrigPrice = fvautobuyStock.FindControl("tbTriggerPrice") as TextBox;
            Label lblTrigPrice = fvConfrimautobuy.FindControl("lblTriggerPrice") as Label;
            float buyPrice = float.Parse(tbTrigPrice.Text);//price
            Stock stk = new Stock(lblSymbol.Text,buyPrice,(Int32)numSharesToautobuy);

            string username = System.Web.HttpContext.Current.User.Identity.Name.ToString();//loginname
            //float totPrice = stk.Price * float.Parse(lblNumShares.Text);
            //Schedule autobuy in transaction table

            AutoTransaction autotrans = new AutoTransaction("AUTOBUY",stk, (Int32) numSharesToautobuy,username,trend,buyPrice);
            autotrans.Execute();
            Response.Redirect("~/Pages/AutoBuy.aspx");
        }

        protected void cancelautobuy(object sender, EventArgs e)
        {
            //Redirect back to original autobuy stock page
            Response.Redirect("~/Pages/AutoBuy.aspx");
        }

        protected void tbStockSym_TextChanged(object sender, EventArgs e)
        {
            Stock stk = new Stock(tbStockSym.Text);
            if (stk.CompanyName.Equals("NULL"))
            {
                //Unknown symbol
                Label lbl = new Label();
                lbl.Text = "Stock sybmol " + stk.Symbol + " not found. ";
                lbl.Font.Bold = true;
                lbl.ForeColor = System.Drawing.Color.Red;
                phFailSearch.Controls.Add(lbl);
            }
        }
    }
}